﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FzWeb.Models
{
    public class CompanyService
    {
        public Entities db
        {
            get
            {
                if (HttpContext.Current.Session["db"] == null)
                {
                    HttpContext.Current.Session["db"]= new Entities();
                }
                return HttpContext.Current.Session["db"] as FzWeb.Models.Entities;
            }
        }

        public IList<Company> GetRecommendCompany()
        {   
            
            return db.Company.Take(10).ToList();
        }

        public Company GetCompany(int id)
        {
            return db.Company.Include("user").Include("post").First(c => c.CompanyId == id);
        }

        public IList<Company> GetCompanyList()
        {
            return db.Company.Include("user").ToList();
        }

        public Settlement GetLastSettlement(Person_Job_History pjh)
        {
            return db.Settlement.Where(c => c.Company.CompanyId == pjh.Company.CompanyId && c.Post.PostId == pjh.Post.PostId && c.Person.PersonId == pjh.Person.PersonId).OrderByDescending(c=>c.SettlementId).First();
        }
        /// <summary>
        /// 获得公司所有数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Company GetComanyAll(int id)
        {
            return db.Company.Include("user").Include("post").Include("Company_Resume").Include("Company_Resume.person_resume").First(c => c.CompanyId == id);
        }

        /// <summary>
        /// 获得人才库数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Company GetCompanyCompetentPerson(int id)
        {
            return db.Company.Include("Company_Resume").Include("Company_Resume.person_resume").Include("Company_Resume.person_resume.person").First(c => c.CompanyId == id);
        }

       
        /// <summary>
        /// 获得人才库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Company_Resume GetCompayResume(int id)
        {
            return db.Company_Resume.First(c => c.CRId == id);
        }

        public void CreateCompanyResume(Company_Resume cr) {
            db.AddToCompany_Resume(cr);
            db.SaveChanges();
        }

        public IList<Company_Resume> GetCompayResumeList(Func<Company_Resume, bool> predict)
        {
            return db.Company_Resume.Where(predict).OrderBy(c => c.AddTime).ToList();
        }

        public IList<Company_Resume> GetCompayResumeList(int? companyid)
        {
            if (companyid.HasValue)
            {
                return db.Company_Resume.Include("company").Include("person_resume").Include("person_resume.person").Where(c=>c.Company.CompanyId==Convert.ToInt32(companyid)).OrderBy(c => c.AddTime).ToList();
            }
            else
            {
                return db.Company_Resume.Include("company").Include("person_resume").Include("person_resume.person").OrderBy(c => c.AddTime).ToList();
            }
        }

        public void DelCompanyResume(int id)
        {
            db.DeleteObject(GetCompayResume(id));
            db.SaveChanges();
        }

        /// <summary>
        /// 获得公司目录
        /// </summary>
        /// <returns></returns>
        public IList<Menu> GetCompanyMenu()
        {
            return db.Menu.Include("childs").Include("parent").Where(c => c.Type == 0 && c.Parent == null).ToList();
        }

        public Menu GetCompanyMenu(string url)
        {
            return db.Menu.Include("childs").Include("parent").First(c => c.Type == 0 && c.Url==url);
        }

        public void saveChanges(Company c)
        {
            db.ApplyPropertyChanges(c.EntityKey.EntitySetName, c);
            db.SaveChanges();
        }

        public void saveChanges()
        {
            db.SaveChanges();
        }

        public void UpdateCompany(Company c) {
            var ocomapny = db.Company.Where(d => d.CompanyId == c.CompanyId).FirstOrDefault();
            db.ApplyPropertyChanges(ocomapny.EntityKey.EntitySetName, c);
            db.SaveChanges();
        }

        public Post GetPost(int id)
        {
            return db.Post.First(c => c.PostId == id);
        }

        public IList<Post> GetCompanyPost(int companyId)
        {
            return db.Company.Include("post").Include("post.Post_param").First(c => c.CompanyId == companyId).Post.ToList();
        }


        /// <summary>
        /// 获得在岗人员
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public IList<Person_Job_History> GetWorkPerson(int companyId)
        {
            return db.Company.Include("Person_Job_History").Include("Person_Job_History.post").Include("Person_Job_History.person").First(c=>c.CompanyId == companyId).Person_Job_History.Where(c=>Convert.ToBoolean(c.CompanyDelMark) == false && c.Status == 1).ToList();
        }

        public IList<Person_Job_History> GetWorkPersonList()
        {
            return db.Person_Job_History.Include("company").Include("person").Include("post").ToList();
        }


        /// <summary>
        /// 获得离职人员
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public IList<Person_Job_History> GetWorkPersonDimission(int companyId)
        {
            return db.Company.Include("Person_Job_History").Include("Person_Job_History.post").Include("Person_Job_History.person").First(c => c.CompanyId == companyId).Person_Job_History.Where(c => Convert.ToBoolean(c.CompanyDelMark) == false && c.Status == 2).ToList();
        }

        public IList<Company_Advances> GetCompanyAdvancesList(Func<Company_Advances, bool> predict)
        {
            return db.Company_Advances.Where(predict).ToList();
        }

        public IList<Company_statements> GetCompanyStatementsList(Func<Company_statements, bool> predict)
        {
            return db.Company_statements.Where(predict).ToList();
        }


        public Company_statements GetCompanyStatements(Func<Company_statements, bool> predict)
        {
            return db.Company_statements.First(predict);
        }

        public Company_Advances GetCompanyAdvances(Func<Company_Advances, bool> predicate)
        {
            return db.Company_Advances.First(predicate);
        }




    }
}
